/*
    MainView extends JPanel implements ActionListener
        :This is the panel seen once the program starts with the options (play game, view stats, admin mode)
        Attributes
        Methods
*/

//Main view acts as both a JPanel and an ActionListener
//  this way I don't have to create a sub class to act as the action listener
//The Jpanel side of this class uses layers of grids to position the buttons exactly where I want them


//import only the stuff we need
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Graphics;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Color;
import java.awt.Image;
import java.awt.Font;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import javax.imageio.ImageIO;

public class MainView extends JPanel implements ActionListener{
	
	//declare some components we would like to have access to after the constructor is finished
	private Image background;
	private Image scaledbg;
	private Dimension prevsize;
	private JButton playBtn;
	private JButton statsBtn;
	private JButton adminBtn;
	//common colors
	public static Color bgnorm = new Color(188, 143, 70);
	public static Color fgnorm = new Color(56, 5, 3);
	public static Color bgalt1 = new Color(221, 173, 98);
	public static Color fgalt1 = new Color(93, 15, 2);
	public static Color fgalt2 = new Color(242, 79, 1);
	
	//our big contructor
	public MainView() {
		
		//we need to call our ancestor
		super();
		
		//we are gonna need dummy components to act as seat fillers
		ArrayList<JPanel> dummies = new ArrayList<JPanel>();
		int dummycount = 5;
		for (int i=0; i<dummycount; ++i){
			JPanel temp = new JPanel();
			temp.setVisible(false);
			dummies.add(temp);
		}
		//nobody.setPreferredSize(new Dimension(2000,2000));
		
		//we are gonna need some temporary components for proper structure
		JPanel temp1;
		JPanel temp2;
		
		//define a layout
		//I know we want the buttons to be centered
		//  so I will create a grid with 3 columns of equal size
		//  then I will split the center column into 4 rows
		//  I will split the 3rd row into 3 rows and place the buttons
		//  dummy components will need to be added since GridLayout is silly
		//  and won't allow placement into a specific grid location
		this.setLayout(new GridLayout(1,3));
		//column 1 is filler
			this.add(dummies.get(0));
		//column 2 is significant
			temp1 = new JPanel();
			temp1.setOpaque(false);
			temp1.setLayout(new GridLayout(4,1));
			//row 1 is filler
				temp1.add(dummies.get(2));
			//row 2 is filler
				temp1.add(dummies.get(3));
			//row 3 is significant
				temp2 = new JPanel();
				temp2.setOpaque(false);
				temp2.setLayout(new GridLayout(3,1));
				//sub row 1 is game button
					this.playBtn = new JButton();
					//setup action listening
					this.playBtn.addActionListener(this);
					//setup button text
					this.playBtn.setText("Play Game");
					this.playBtn.setFont(new Font("Franklin Gothic Heavy", Font.PLAIN, 20));
					//setup colors button and text respectively
					this.playBtn.setBackground(MainView.bgnorm);
					this.playBtn.setForeground(MainView.fgnorm);
					//setup visibility
					this.playBtn.setOpaque(true);
					this.playBtn.setVisible(true);
					temp2.add(this.playBtn);
				//sub row 2 is stats button
					this.statsBtn = new JButton();
					//setup action listening
					this.statsBtn.addActionListener(this);
					//setup button text
					this.statsBtn.setText("View Stats");
					this.statsBtn.setFont(new Font("Franklin Gothic Heavy", Font.PLAIN, 20));
					//setup colors button and text respectively
					this.statsBtn.setBackground(MainView.bgnorm);
					this.statsBtn.setForeground(MainView.fgnorm);
					//setup visibility
					this.statsBtn.setOpaque(true);
					this.statsBtn.setVisible(true);
					temp2.add(this.statsBtn);
				//sub row 3 is admin button
					this.adminBtn = new JButton();
					//setup action listening
					this.adminBtn.addActionListener(this);
					//setup button text
					this.adminBtn.setText("Admin Mode");
					this.adminBtn.setFont(new Font("Franklin Gothic Heavy", Font.PLAIN, 20));
					//setup colors button and text respectively
					this.adminBtn.setBackground(MainView.bgnorm);
					this.adminBtn.setForeground(MainView.fgnorm);
					//setup visibility
					this.adminBtn.setOpaque(true);
					this.adminBtn.setVisible(true);
					temp2.add(this.adminBtn);
				temp1.add(temp2);

			//row 4 is filler
				temp1.add(dummies.get(4));
			this.add(temp1);
		//column 3 is filler
			this.add(dummies.get(1));
		
		//set a reasonable size to match the ratio of our splash image
		this.setPreferredSize(new Dimension(500, 375));
		
		//try to get our splash image
		//if we cannot retrieve the image, it is somewhat serious
		//but I don't know what we could do from that point on :(
		try{
			background = ImageIO.read(new File("images\\SplashMedium.jpg"));
		}catch (IOException EXC){
			//this likely means there was some problem accessing the image file, use default
			background = new UIController.NotFoundImage(500, 375);
		}catch (IllegalArgumentException EXC){
			//this likely means that for some reason we couldn't create the File object, use default
			background = new UIController.NotFoundImage(500, 375);
		}
		
		//setup visibilities
		this.setVisible(true);
		this.setOpaque(true);
		
	}
	
	//override this so that we can customize drawing of the component
	//specifically so that we always have our pretty splash screen behind us :D
	public void paintComponent(Graphics g) {
		
		//call our ancestor
		super.paintComponent(g);
		
		System.out.println("$1MainView: painting...");
		
		//getSize is a retarded method, as such it requires
		//  we pass a Dimension object that it will use by referrence to store the size
		//the retarded part is that it also returns the same Dimension object
		//  go figure...
		Dimension panelsize = new Dimension(0,0);
		this.getSize(panelsize);
		
		//check if previous size has been declared yet
		if (this.prevsize == null){
			this.prevsize = new Dimension(panelsize);
		}
		
		//here is where we draw our Image
		//  the first 4 ints represent the destination points
		//  the second 4 ints represent the source points
		//    its a kinda stupid implementation if you ask me
		//    all it is doing is translating and scaling the source image 
		//    so that it will fit the destination
		//  the final null value is actually known as the ImageObserver
		//    this is a strange little object that can act as an event handler
		//    for our image and tell us information about the image once it becomes available
		//    requests to our image for information are dealt with asynchronously
		//    if the information is not yet available, the requests return dummy values
		//    and tell the ImageObserver to inform everyone when the info is actually available
		//    for our purposes, we shouldn't need to have an ImageObserver
		/*
		g.drawImage(background,0,0,(int)panelsize.getWidth(),(int)panelsize.getHeight(),
					0,0,background.getWidth(null),background.getHeight(null),
					null);
		*/
		//nvm that, instead I will use specific scaling
		//check if we need to rescale the image
		// this can occur if the component size changed
		// or it can occur if the image has not been scaled yet
		if (this.scaledbg == null || !panelsize.equals(this.prevsize)){
			this.scaledbg = background.getScaledInstance(panelsize.width,
			panelsize.height, Image.SCALE_SMOOTH);
		}
		g.drawImage(scaledbg, 0, 0, null);
		
		//reset the previous scale size
		this.prevsize = new Dimension(panelsize);
		
	}
	
	//we need to define this method since it is fully abstrct by default
	//  this will be used to listen for button clicks and call particular UIController methods
	//  specifically we should call gameStart(), statsStart(), or adminStart()
	//  and then let the UIController handle everything from that point on
	public void actionPerformed(ActionEvent EVE) {
		
		//check to see who caused the event to be called
		Object caller = EVE.getSource();
		if (caller.equals(playBtn)){
			//Play Game Button
			UIController.changeState("GameLogon");
		}else if(caller.equals(statsBtn)){
			//View Stats Button
			UIController.changeState("StatsLogon");
		}else if(caller.equals(adminBtn)){
			//Admin Mode Button
			UIController.changeState("AdminLogon");
		}
		
	}
	
}